package com.news.wemedia.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.news.model.common.dtos.PageResponseResult;
import com.news.model.common.dtos.ResponseResult;
import com.news.model.common.enums.AppHttpCodeEnum;
import com.news.model.wemedia.dtos.WmChannelDto;
import com.news.model.wemedia.pojos.WmChannel;
import com.news.model.wemedia.pojos.WmNews;
import com.news.wemedia.mapper.WmChannelMapper;
import com.news.wemedia.mapper.WmNewsMapper;
import com.news.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    @Autowired
    private WmChannelMapper wmChannelMapper;

    @Autowired
    private WmNewsMapper wmNewsMapper;
    /**
     * 查询所有频道
     * @return
     */
    @Override
    public ResponseResult findAll() {
        return ResponseResult.okResult(list());
    }

    /**
     * 分页查询所有频道
     * @param wmChannelDto
     * @return
     */
    @Override
    public ResponseResult listPage(WmChannelDto wmChannelDto) {

        if (wmChannelDto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        IPage page =new Page(wmChannelDto.getPage(),wmChannelDto.getSize());

        page = page(page, Wrappers.<WmChannel>lambdaQuery()
                .like(WmChannel::getName,wmChannelDto.getName())
                .orderByDesc(WmChannel::getCreatedTime)
        );

        ResponseResult responseResult = new PageResponseResult(wmChannelDto.getPage()
                , wmChannelDto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    /**
     * 新增频道
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult saveOne(WmChannel wmChannel) {

        if(wmChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        WmChannel wmChannel1 = wmChannelMapper.selectOne(Wrappers.<WmChannel>lambdaQuery()
                .eq(WmChannel::getName, wmChannel.getName()));
        if (wmChannel1 != null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,wmChannel.getName()+"已存在");
        }
        wmChannel.setCreatedTime(new Date());
        save(wmChannel);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 根据id删除频道
     * @param id
     * @return
     */
    @Override
    public ResponseResult delOne(Integer id) {

        if (id == null ){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        WmChannel wmChannel = wmChannelMapper.selectById(id);
        if (wmChannel.getStatus()==true){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,wmChannel.getName()+"正在启用在不可删除");
        }
        wmChannelMapper.deleteById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改频道
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult updateOne(WmChannel wmChannel) {

        if (wmChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        List<WmNews> wmNews = wmNewsMapper.selectList(Wrappers.<WmNews>lambdaQuery()
                .eq(WmNews::getChannelId, wmChannel.getId()));
        log.info("wmNews:{}",wmNews);
        if (wmNews !=null && wmNews.size() >0 && wmChannel.getStatus()==false){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,wmChannel.getName()+"已被引用不可禁用");
        }

        wmChannelMapper.updateById(wmChannel);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}